﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    public class DocClsScore
    {
        public DocClsScore()
        { }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(List<ElingOA6_Admin_DSJCJ.Model.DocClsScore> list)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            foreach (ElingOA6_Admin_DSJCJ.Model.DocClsScore model in list)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update t_docClsScore set ");
                strSql.Append("FIsScore=@FIsScore,");
                strSql.Append("FEnrollScore=@FEnrollScore");
                strSql.Append(" where FEntityNo=@FEntityNo and FDocClsCd=@FDocClsCd and FYear=@FYear ");
                SqlParameter[] parameters = {
					new SqlParameter("@FIsScore", SqlDbType.Decimal,5),
					new SqlParameter("@FEnrollScore", SqlDbType.Decimal,5),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDocClsCd", SqlDbType.VarChar,25),
					new SqlParameter("@FYear", SqlDbType.Decimal,5)};
                parameters[0].Value = model.FIsScore;
                parameters[1].Value = model.FEnrollScore;
                parameters[2].Value = model.FEntityNo;
                parameters[3].Value = model.FDocClsCd;
                parameters[4].Value = model.FYear;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithIndentity(cmdList, 30 * 60);
        }

        /// <summary>
        /// 获取信息考核分定义列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>数据列表</returns>
        public DataSet GetDocClsScoreList(string strWhere)
        {
            string sql = "select *  FROM v_DocClsScore ";

            if (strWhere != "")
            {
                sql += " where " + strWhere;
            }

            sql += " order by FSeq,FDocClsCd";
            return DbHelperSQL.Query(sql);
        }


        public DataSet ExecSql()
        {
            string sql = "select FYear  FROM t_docClsScore  group by FYear order by FYear desc";
            return DbHelperSQL.Query(sql);
        }

        /// <summary>
        /// 获取所有的年份，按年份降序
        /// <creator>sunlj</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public DataSet getYears(string strEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FYear  FROM t_docClsScore WHERE FEntityNo=@FEntityNo group by FYear order by FYear desc");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = strEntityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        public void ExecPro(string EntityNo, int year)
        {
            SqlParameter[] para = new SqlParameter[]
             {
                  new SqlParameter("@EntityNo",EntityNo),
                  new SqlParameter("@yyyy",year)
             };
            DbHelperSQL.RunProcedure("sp_initDocClsScroe", para);
        }

        /// <summary>
        /// 根据年份获取所有需要考核的信息考核分定义
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <param name="decYear"></param>
        /// <returns></returns>
        public DataSet getListByYearAndIsScore(string strEntityNo, decimal decYear)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM t_docClsScore WHERE FEntityNo=@FEntityNo and FYear=@FYear and FIsScore=1");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
                    new SqlParameter("@FYear", SqlDbType.Decimal)};
            parameters[0].Value = strEntityNo;
            parameters[1].Value = decYear;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.DocClsScore DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.DocClsScore model = new ElingOA6_Admin_DSJCJ.Model.DocClsScore();
            if (row != null)
            {
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FDocClsCd"] != null)
                {
                    model.FDocClsCd = row["FDocClsCd"].ToString();
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FIsScore"] != null && row["FIsScore"].ToString() != "")
                {
                    model.FIsScore = decimal.Parse(row["FIsScore"].ToString());
                }
                if (row["FWriteScore"] != null && row["FWriteScore"].ToString() != "")
                {
                    model.FWriteScore = decimal.Parse(row["FWriteScore"].ToString());
                }
                if (row["FEnrollScore"] != null && row["FEnrollScore"].ToString() != "")
                {
                    model.FEnrollScore = decimal.Parse(row["FEnrollScore"].ToString());
                }
                if (row["FExamineScore"] != null && row["FExamineScore"].ToString() != "")
                {
                    model.FExamineScore = decimal.Parse(row["FExamineScore"].ToString());
                }
                if (row["FWriteNum"] != null && row["FWriteNum"].ToString() != "")
                {
                    model.FWriteNum = int.Parse(row["FWriteNum"].ToString());
                }
                if (row["FWriteScore1"] != null && row["FWriteScore1"].ToString() != "")
                {
                    model.FWriteScore1 = decimal.Parse(row["FWriteScore1"].ToString());
                }
                if (row["FWriteScore2"] != null && row["FWriteScore2"].ToString() != "")
                {
                    model.FWriteScore2 = decimal.Parse(row["FWriteScore2"].ToString());
                }
                if (row["FWriteScore3"] != null && row["FWriteScore3"].ToString() != "")
                {
                    model.FWriteScore3 = decimal.Parse(row["FWriteScore3"].ToString());
                }
                if (row["FWriteScore4"] != null && row["FWriteScore4"].ToString() != "")
                {
                    model.FWriteScore4 = decimal.Parse(row["FWriteScore4"].ToString());
                }
                if (row["FWriteNum1"] != null && row["FWriteNum1"].ToString() != "")
                {
                    model.FWriteNum1 = int.Parse(row["FWriteNum1"].ToString());
                }
                if (row["FWriteNum2"] != null && row["FWriteNum2"].ToString() != "")
                {
                    model.FWriteNum2 = int.Parse(row["FWriteNum2"].ToString());
                }
                if (row["FWriteNum3"] != null && row["FWriteNum3"].ToString() != "")
                {
                    model.FWriteNum3 = int.Parse(row["FWriteNum3"].ToString());
                }
                if (row["FWriteNum4"] != null && row["FWriteNum4"].ToString() != "")
                {
                    model.FWriteNum4 = int.Parse(row["FWriteNum4"].ToString());
                }
            }
            return model;
        }
        /// <summary>
        /// 获取最大的年数
        /// <creator>wangl</creator>
        /// <date>2013-11-12</date>
        /// </summary>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public int GetMaxYear(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select MAX(FYear)  FROM t_docClsScore WHERE FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = entityNo;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                 return int.Parse(obj.ToString());
            }
            else
            {
                return DateTime.Now.Year;
            }

        }

        /// <summary>
        /// 年度结转
        /// <creator>wangl</creator>
        /// <date>2013-11-12</date>
        /// </summary>
        /// <param name="entityNo"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public int YearTurn(string entityNo, decimal year)
        {
            int result = -1;
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = {
                    new SqlParameter("@FYear", SqlDbType.Int, 4),
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
                                          };
            parameters[0].Value = year;
            parameters[1].Value = entityNo;
            try
            {
                DbHelperSQL.RunProcedure("sp_YearTransferByDoc", parameters, "ds", 60 * 30);
                result = 1;
            }
            catch
            {
                result = -1;
            }
            return result;
        }
        /// <summary>
        /// 年度反结转
        /// <creator>wangl</creator>
        /// <date>2013-11-12</date>
        /// </summary>
        /// <param name="year"></param>
        /// <param name="FStatus"></param>
        /// <returns></returns>
        public int YearTurnChange(string entityNo, decimal year)
        {
            int result = -1;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_docClsScore where FYear=@FYear and FEntityNo=@FEntityNo;");
            strSql.Append("delete from t_chkInfoCls where FYear=@FYear and FEntityNo=@FEntityNo;");
            strSql.Append("delete from t_chkInfoStdSuper where FYear=@FYear and FEntityNo=@FEntityNo;");
            strSql.Append("delete from t_chkInfoStdLocal where FYear=@FYear and FEntityNo=@FEntityNo;");
            SqlParameter[] parameters = {
                    new SqlParameter("@FYear", SqlDbType.Decimal, 4),
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
                                         };
            parameters[0].Value = year;
            parameters[1].Value = entityNo;
            try
            {
                DbHelperSQL.ExecuteSqlByTime(strSql.ToString(), 60 * 30, parameters);
                result = 1;
            }
            catch
            {
                result = -1;
            }
            return result;

        }
    }
}
